import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

import "../../config/theme_vars.dart";

class RadioButtonGroup extends StatefulWidget {
  final List<Map<String, dynamic>> groupList;
  final Function(String)? onChange;
  final String? defaultValue;

  const RadioButtonGroup({
    super.key, 
    required this.groupList, 
    this.onChange,
    this.defaultValue,
  });

  @override
  State<StatefulWidget> createState() => _RadioButtonGroupState();
}

class _RadioButtonGroupState extends State<RadioButtonGroup> with SingleTickerProviderStateMixin {
  late String active;
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    active = widget.defaultValue ?? widget.groupList[0]["value"];
    
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 150),
      vsync: this,
    );
    
    _scaleAnimation = Tween<double>(begin: 1.0, end: 0.95).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeInOut,
      ),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  Widget _buildRadioButton({required String label, required String value, required String active}) {
    final bool isActive = active == value;
    
    return AnimatedContainer(
      duration: const Duration(milliseconds: 200),
      // margin: const EdgeInsets.symmetric(horizontal: 4),
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: isActive ? ThemeVars.colorPrimary : Colors.white,
        // borderRadius: BorderRadius.circular(4),
        boxShadow: isActive ? [
          BoxShadow(
            color: ThemeVars.colorPrimary.withOpacity(0.3),
            blurRadius: 1,
            offset: const Offset(0, 0),
          )
        ] : [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 1,
            offset: const Offset(0, 0),
          )
        ],
      ),
      child: Text(
        label,
        style: TextStyle(
          fontSize: 12,
          fontWeight: isActive ? FontWeight.w500 : FontWeight.normal,
          color: isActive ? Colors.white : ThemeVars.colorTextSec,
        ),
      ),
    );
  }

  /// 构建单选按钮组
  Widget _buildRadioButtonGroup(List groupList) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: List.generate(groupList.length, (index) {
        Map item = groupList[index];
        return ScaleTransition(
          scale: _scaleAnimation,
          child: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              final String newValue = item["value"];
              if (active != newValue) {
                _animationController.forward().then((_) {
                  _animationController.reverse();
                });
                
                setState(() {
                  active = newValue;
                });
                
                if (widget.onChange != null) {
                  widget.onChange!(newValue);
                }
              }
            },
            child: _buildRadioButton(
              label: item["label"], 
              value: item["value"], 
              active: active,
            ),
          ),
        );
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildRadioButtonGroup(widget.groupList);
  }
}
